package com.cjq.mall.wallet.service.impl;

import com.cjq.mall.wallet.mapper.BusinessDetailMapper;
import com.cjq.mall.wallet.mapper.BusinessTypeMapper;
import com.cjq.mall.wallet.mapper.WalletMapper;
import com.cjq.mall.wallet.pojo.dto.WalletDTO;
import com.cjq.mall.wallet.pojo.dto.WalletRefundDTO;
import com.cjq.mall.wallet.pojo.dto.WalletUpdateDTO;
import com.cjq.mall.wallet.pojo.entity.BusinessDetail;
import com.cjq.mall.wallet.pojo.vo.BusinessDetailVO;
import com.cjq.mall.wallet.pojo.vo.BusinessTypeVO;
import com.cjq.mall.wallet.pojo.vo.WalletVO;
import com.cjq.mall.wallet.service.WalletService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class WalletServiceImpl implements WalletService {

    @Autowired
    private WalletMapper walletMapper;

    @Autowired
    private BusinessDetailMapper businessDetailMapper;

    @Autowired
    private BusinessTypeMapper businessTypeMapper;


    @Override
    public WalletVO getWallet(Long userId) {
        WalletVO walletVO = walletMapper.selectWalletByUserId(userId);

        return walletVO;
    }

    /**
     * 消费
     *
     */
    @Transactional
    @Override
    public void consume(WalletDTO walletDTO) {
        // 验证支付密码



        // 收集信息，需要修改余额的信息和新增交易明细的信息
        // 先获取双方用户余额
        WalletVO walletVO = walletMapper.selectWalletByUserId(walletDTO.getUserId());
        WalletVO walletVO2 = walletMapper.selectWalletByUserId(walletDTO.getToUid());
        if(walletVO == null || walletVO2 == null){
            // 此处抛出自定义异常
        }

        // 获取交易类型详细信息
        BusinessTypeVO businessTypeVO = businessTypeMapper.selectBusinessTypeById(walletDTO.getType());


        // 修改钱包余额的两个对象
        WalletUpdateDTO walletUpdateDTO = new WalletUpdateDTO();
        WalletUpdateDTO walletUpdateDTO2 = new WalletUpdateDTO();
        walletUpdateDTO.setUserId(walletDTO.getUserId());
        walletUpdateDTO2.setUserId(walletDTO.getToUid());

        BigDecimal money = walletVO.getMoney();
        BigDecimal money2 = walletVO2.getMoney();
        if(businessTypeVO.getSign() == 0){
            money = walletVO.getMoney().subtract(walletDTO.getBusinessMoney());
            money2 = walletVO2.getMoney().add(walletDTO.getBusinessMoney());
        } else if(businessTypeVO.getSign() == 1){
            money = walletVO.getMoney().add(walletDTO.getBusinessMoney());
            money2 = walletVO2.getMoney().subtract(walletDTO.getBusinessMoney());
        }

        if(money.intValue() < 0 || money2.intValue() < 0){
            // 此处抛出自定义异常
        }
        walletUpdateDTO.setMoney(money);
        walletUpdateDTO2.setMoney(money2);


        // 交易明细对象
        BusinessDetail businessDetail = new BusinessDetail();
        BusinessDetail businessDetail2 = new BusinessDetail();

        String recordSn = UUID.randomUUID().toString();
        String recordSn2 = UUID.randomUUID().toString();
        LocalDateTime nowTime = LocalDateTime.now();

        businessDetail.setRecordSn(recordSn);
        businessDetail.setUserId(walletDTO.getUserId());
        businessDetail.setUserName(walletVO.getUserName());
        businessDetail.setBusinessMoney(walletDTO.getBusinessMoney());
        businessDetail.setType(walletDTO.getType());
        businessDetail.setToUid(walletDTO.getToUid());
        businessDetail.setToName(walletVO2.getUserName());
        businessDetail.setMoney(money);
        businessDetail.setGmtBusiness(nowTime);

        businessDetail2.setRecordSn(recordSn2);
        businessDetail2.setUserId(walletDTO.getToUid());
        businessDetail2.setUserName(walletVO2.getUserName());
        businessDetail2.setBusinessMoney(walletDTO.getBusinessMoney());
        businessDetail2.setType(walletDTO.getType());
        businessDetail2.setToUid(walletDTO.getUserId());
        businessDetail2.setToName(walletVO.getUserName());
        businessDetail2.setMoney(money2);
        businessDetail2.setGmtBusiness(nowTime);


        // 修改余额
        int rows = walletMapper.upDateWallet(walletUpdateDTO);
        if(rows != 1){
            // 抛出自定义异常,触发回滚
        }
        int rows2 = walletMapper.upDateWallet(walletUpdateDTO2);
        if(rows2 != 1){
            // 抛出自定义异常,触发回滚
        }

        // 新增交易明细
        int rows3 = businessDetailMapper.saveBusinessDetail(businessDetail);
        if(rows3 != 1){
            // 抛出自定义异常,触发回滚
        }
        int rows4 = businessDetailMapper.saveBusinessDetail(businessDetail2);
        if(rows4 != 1){
            // 抛出自定义异常,触发回滚
        }
    }


    /**
     * 退款,退款的用户是执行方,对方用户是退款发起方
     * @param walletRefundDTO
     */
    @Transactional
    @Override
    public void refund(WalletRefundDTO walletRefundDTO) {
        // 验证支付密码

        // 收集信息，需要修改余额的信息和新增交易明细的信息
        // 先获取用户余额
        WalletVO walletVO = walletMapper.selectWalletByUserId(walletRefundDTO.getUserId());
        WalletVO walletVO2 = walletMapper.selectWalletByUserId(walletRefundDTO.getToUid());
        if(walletVO == null || walletVO2 == null){
            // 此处抛出自定义异常
        }

        // 修改钱包余额的对象
        WalletUpdateDTO walletUpdateDTO = new WalletUpdateDTO();
        WalletUpdateDTO walletUpdateDTO2 = new WalletUpdateDTO();
        walletUpdateDTO.setUserId(walletRefundDTO.getUserId());
        walletUpdateDTO2.setUserId(walletRefundDTO.getToUid());

        BigDecimal money = walletVO.getMoney().subtract(walletRefundDTO.getRefundMoney());
        BigDecimal money2 = walletVO2.getMoney().add(walletRefundDTO.getRefundMoney());
        if(money.intValue() < 0 || money2.intValue() < 0){
            // 此处抛出自定义异常
        }
        walletUpdateDTO.setMoney(money);
        walletUpdateDTO2.setMoney(money2);

        // 交易明细对象
        BusinessDetail businessDetail = new BusinessDetail();
        BusinessDetail businessDetail2 = new BusinessDetail();
        String recordSn = UUID.randomUUID().toString();
        String recordSn2 = UUID.randomUUID().toString();
        LocalDateTime nowTime = LocalDateTime.now();

        businessDetail.setRecordSn(recordSn);
        businessDetail.setUserId(walletRefundDTO.getUserId());
        businessDetail.setUserName(walletVO.getUserName());
        businessDetail.setBusinessMoney(walletRefundDTO.getRefundMoney());
        businessDetail.setType(walletRefundDTO.getType());
        businessDetail.setToUid(walletRefundDTO.getToUid());
        businessDetail.setToName(walletVO2.getUserName());
        businessDetail.setMoney(money);
        businessDetail.setGmtBusiness(nowTime);

        businessDetail2.setRecordSn(recordSn2);
        businessDetail2.setUserId(walletRefundDTO.getToUid());
        businessDetail2.setUserName(walletVO2.getUserName());
        businessDetail2.setBusinessMoney(walletRefundDTO.getRefundMoney());
        businessDetail2.setType(walletRefundDTO.getType());
        businessDetail2.setToUid(walletRefundDTO.getUserId());
        businessDetail2.setToName(walletVO.getUserName());
        businessDetail2.setMoney(money2);
        businessDetail2.setGmtBusiness(nowTime);

        // 修改余额
        int rows = walletMapper.upDateWallet(walletUpdateDTO);
        if(rows != 1){
            // 抛出自定义异常,触发回滚
        }
        int rows2 = walletMapper.upDateWallet(walletUpdateDTO2);
        if(rows2 != 1){
            // 抛出自定义异常,触发回滚
        }

        // 新增交易明细
        int rows3 = businessDetailMapper.saveBusinessDetail(businessDetail);
        if(rows3 != 1){
            // 抛出自定义异常,触发回滚
        }
        int rows4 = businessDetailMapper.saveBusinessDetail(businessDetail2);
        if(rows4 != 1){
            // 抛出自定义异常,触发回滚
        }

    }

    @Override
    public PageInfo<BusinessDetailVO> getBusinessDetail(Long userId, Integer months, Integer page, Integer pageSize) {
        LocalDateTime noeTime = LocalDateTime.now();
        LocalDateTime startTime = noeTime.minusMonths(months);
        LocalDateTime endTime = noeTime;

        // 设置分页条件
        PageHelper.startPage(page, pageSize);

        // 查询
        List<BusinessDetailVO> list = businessDetailMapper.selectBusinessDetailByTime(userId, startTime, endTime);

        return new PageInfo<BusinessDetailVO>(list);
    }


}
